package com.lee.token.controller;

import com.lee.token.model.User;
import com.lee.token.utils.TokenUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @PostMapping(value = "/login")
    // 前端发送json
    public String login(@RequestBody User user) {
        String token = TokenUtil.sign(user);
        log.info("-------:" + token);
        return token;
    }

    // 测试获取数据，在此之前进行token验证
    @GetMapping(value = "/test")
    @ResponseBody
    public String test() throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data", "data");
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
